<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/ukkepuk/source/php/classes/validation/ActionResult.php');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of InvoiceDao
 *
 * @author virtual
 */
class InvoiceDao {
    private $mysqli;

    public function __construct(){
        //echo "construct";
        $this->mysqli = new mysqli('localhost', 'root', 'password', 'ukkepuk');
        if($this->mysqli->connect_error){
            die('Connect Error (' . $this->mysqli->connec_errno . ') ' . $mysqli->connect_error);
        }

        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
        }

    }
    public function __destruct(){
        //echo "destruct";
        $this->mysqli->close();
    }
    
    public function readKidInvoiceXml($kidId, $year, $month) {
        $firstDayOfMonth = mktime(0, 0, 0, $month, 1, $year);
        $yearMonthNow =  date("Ym",$firstDayOfMonth);
        $constFixedPrice = "FIXED_PRICE";
        //find fixed price and kid info
        $kidsparams = $this->mysqli->prepare("SELECT kp.VALUE, k.NAME, k.FIRST_NAME, a.STREET, a.NUMBER, a.POSTAL, a.CITY  FROM kid_parameters kp, kids k, address a WHERE kp.KIDS_ID = k.ID AND a.ID = k.ADDRESS_ID AND kp.KIDS_ID = ? AND kp.`KEY` = ?");
        $kidsparams->bind_param("is", $kidId, $constFixedPrice);
        $kidsparams->execute();
		$kidsparams->bind_result($price, $kidName, $kidFirstName, $kidStreet, $kidNumber, $kidPostal, $kidCity);
        $kidsparams->fetch();
        $kidsparams->close();
        
        //findnum of days
        $kidscalendardays = $this->mysqli->prepare("SELECT DATE, START_TIME, END_TIME FROM calendar WHERE KIDS_ID = ? AND date_format(DATE, '%Y%m') = ? ORDER BY DATE");
        $kidscalendardays->bind_param("is", $kidId, $yearMonthNow);
        $kidscalendardays->execute();
		$kidscalendardays->bind_result($date, $starttime, $endtime);
        
        
        $resultCalendarItem = "";
        
        $numrows = 0;
        while($kidscalendardays->fetch()){
            $resultCalendarItem .= "<calendarItem date=\"$date\" starttime=\"$starttime\" endtime=\"$endtime\"/>";
            $numrows++;
        }
        $kidscalendardays->close();
        
        $result = "<invoice kidId=\"$kidId\" name=\"$kidName\" firstname=\"$kidFirstName\" street=\"$kidStreet\" number=\"$kidNumber\" city=\"$kidCity\" postal=\"$kidPostal\" month=\"$month\" year=\"$year\" fixedprice=\"$price\" daysinmonth=\"$numrows\">";
        
        
        $result .= $resultCalendarItem;
        
        
        $result .= "</invoice>";
        
        return $result; 
    }
    
}

?>
